$(function () {
    $("#btnSubmit").on('click', getResult);
});

$(document).ready(function () {

    const $valueSpan = $('#weightSpan');
    const $value = $('#inputWeight');
    $valueSpan.html($value.val());
    $value.on('input change', () => {
        $valueSpan.html($value.val());
    });

    const $valueSpan1 = $('#heightSpan');
    const $value1 = $('#inputHeight');
    $valueSpan1.html($value1.val());
    $value1.on('input change', () => {
        $valueSpan1.html($value1.val());
    });

});

var healthIndexAdvice = [
    'Your health index is {0}, and your physical condition is not good. It is recommended to change your\n' +
    'lifestyle and do more exercise. And adjust the diet, intake more trace elements and high-quality\n' +
    'carbon water. And need to drink less and quit smoking.',
    'Your health index is {0}, and you are in good physical condition. But pay attention to calorie intake,\n' +
    'too much calorie intake will be converted into fat and make people fat.\n',
    'Your health index is {0}, and your physical condition is very good. You have good eating habits and\n' +
    'exercise. Recommendation: Keep it up, but strengthen strength training to better adapt to work\n' +
    'and study.'
]


var trainAdvice = [
    'The amount of exercise is too low, both aerobic exercise and anaerobic\n' +
    'exercise need to be strengthened. You can refer to the course "CARDIO\n' +
    'WORKOUTS" and "STRENGTH TRAINING" to improve your training.',
    'Excessive exercise will cause the muscles to not be able to fully replenish\n' +
    'protein and it will be counterproductive if there is not enough stretching.\n' +
    'Recommend "YOGA & FLEXIBILITY" and "AB WORKOUTS" courses.',
    'Exercise is very good, there is enough aerobic exercise and anaerobic\n' +
    'exercise. Recommend "DANCE WORKOUTS" to improve your own balance\n' +
    'training. And, you can refer to other courses on our website to further\n' +
    'improve yourself.'
]

var nutritionAdvice = [
    'The intake of high-quality carbohydrates, water, vegetables, and protein\n' +
    'are all insufficient. Suggested diet: sweet potatoes, red meat (beef),\n' +
    'broccoli and green peppers. ',
    'Excessive intake of high-quality carbon water, water, vegetables, and\n' +
    'protein. Recommendation: Eat whole-wheat bread instead of staple\n' +
    'food, and your intake of water, vegetables, and protein can be halved. ',
    'Your diet is very healthy and the sources of energy intake are very wide.\n' +
    'Suggestion: Eat more trace elements and fats (high-quality fats, such as\n' +
    'nuts).'
]

function setTrainAdvice(adviceClass) {
    var advice = getTrainAdvice();
    $("#" + adviceClass).text(advice);
}

function setNutritionAdvice(adviceClass) {
    var advice = getNutritionAdvice();
    $("#" + adviceClass).text(advice);
}


function setHealthIndex(adviceClass) {
    var healthIndex = getHealthIndex();
    var advice = getHealthIndexExplanation(healthIndex);
    $("#" + adviceClass).text(advice);
}

function getResult() {

    setTrainAdvice('trainAdvice');
    setNutritionAdvice('nutritionAdvice');
    setHealthIndex('healthIndex');
}


function getHealthIndex() {
    var weightScore = getWeightScore();
    var bmiScore = getBMIScore();
    var lifeStyleScore = getLifeStyleScore();
    var nutritionScore = getNutritionScore();
    var calorieScore = getCalorieScore();
    var healthIndex = weightScore + bmiScore + lifeStyleScore + nutritionScore + calorieScore;
    return healthIndex;
}


function getWeightScore() {
    var weight = $("#inputWeight").val();
    if (weight > 100)
        return 0;
    else if (weight > 80 && weight <= 100)
        return 8;
    else if (weight > 50 && weight <= 80)
        return 18;
    else if (weight <= 50)
        return 20;
}

function getBMIScore() {
    var weight = $("#inputWeight").val();
    var height = $("#inputHeight").val() / 100;
    var bmi = weight / (height * height)
    if (bmi > 18 && bmi < 25)
        return 20;
    else
        return 10;
}

function getLifeStyleScore() {
    var lifeHabit = $("input[name='lifeHabit']:checked").val();
    var mealHabit = $("input[name='mealHabit']:checked").val();
    var mealFullness = $("input[name='mealFullness']:checked").val();
    if (lifeHabit == mealFullness && mealHabit == mealFullness && lifeHabit == mealHabit) {
        return 10;
    } else if (lifeHabit == '4' && mealHabit == '5' && mealFullness == '6') {
        return 20;
    } else if (lifeHabit != mealHabit && lifeHabit != mealFullness && mealHabit != mealFullness) {
        return 8;
    } else
        return 12;

}

function getNutritionScore() {
    var grainsIntake = $("input[name='grainsIntake']:checked").val();
    var vegeIntake = $("input[name='vegeIntake']:checked").val();
    var proteinIntake = $("input[name='proteinIntake']:checked").val();
    var fruitIntake = $("input[name='fruitIntake']:checked").val();
    var waterIntake = $("input[name='waterIntake']:checked").val();

    if (grainsIntake == '1' && vegeIntake == '1' && proteinIntake == '1' && fruitIntake == '1' && waterIntake == '1') {
        return 0;
    } else if (grainsIntake == '3' && vegeIntake == '3' && proteinIntake == '3' && fruitIntake == '3' && waterIntake == '3') {
        return 20;
    } else
        return 10;
}

function getCalorieScore() {
    var cal = $("input[name='junkIntake']:checked").val();
    if (cal == '1')
        return 0;
    else if (cal == '2')
        return 5;
    else if (cal == '3')
        return 10;
    else if (cal == '4')
        return 20;
}


function getHealthIndexExplanation(healthIndex) {
    if (healthIndex >= 0 && healthIndex < 40) {
        return healthIndexAdvice[0].replace("{0}",healthIndex);
    } else if (healthIndex >= 40 && healthIndex < 60) {
        return healthIndexAdvice[1].replace("{0}",healthIndex);
    } else
        return healthIndexAdvice[2].replace("{0}",healthIndex);
}


function getTrainAdvice() {

    var LITValue = $("input[name='LIT']:checked").val();
    var MITValue = $("input[name='MIT']:checked").val();
    var HITValue = $("input[name='HIT']:checked").val();
    if (LITValue == '1' && MITValue == '1' && HITValue == '1') {
        return trainAdvice[0];
    } else if (LITValue == '4' && MITValue == '4' && HITValue == '4') {
        return trainAdvice[1];
    } else
        return trainAdvice[3];
}

function getNutritionAdvice() {
    var grainsIntake = $("input[name='grainsIntake']:checked").val();
    var vegeIntake = $("input[name='vegeIntake']:checked").val();
    var proteinIntake = $("input[name='proteinIntake']:checked").val();
    var fruitIntake = $("input[name='fruitIntake']:checked").val();
    var waterIntake = $("input[name='waterIntake']:checked").val();

    if (grainsIntake == '1' && vegeIntake == '1' && proteinIntake == '1' && fruitIntake == '1' && waterIntake == '1') {
        return nutritionAdvice[0];
    } else if (grainsIntake == '5' && vegeIntake == '4' && proteinIntake == '4' && fruitIntake == '4' && waterIntake == '4') {
        return nutritionAdvice[1];
    } else
        return nutritionAdvice[2];
}

